import React, { Component } from "react"
import { withRouter } from "react-router"
import styles from './login.less'
import CInput from '@/components/input'
import { login, userEquipList } from '@/services'
import Toast from '@/utils/toast';

class Login extends Component {

  constructor(props){
    super(props);
    this.state = {
      phone: '',
      password: '',
    }
  }

  onChange = (e) => {
    // if(e.target.name=="phone"){
    //   e.length == 11 && (/^[1][0-9]{10}$/.test(e))
    // }
    this.setState({
      [e.target.name] : e.target.value
    })
  }

  handleSubmit = async () => {
    try {
      const { phone, password, repassword } = this.state;
      if(!(phone.length == 11 && (/^[1][0-9]{10}$/.test(phone)))){
        return Toast.error('请输入正确的手机号')
      }
      if(!(password)){
        return Toast.error('请输入密码')
      }
      const data = await login({ userName: phone, password: password })
      if(data.code===200){
        const list = await userEquipList();
        if(list.code === 200 && list.data.length>0){
          this.props.history.replace('/')
        }else{
          this.props.history.replace('/bindEquip')
        }
      }
    } catch (error) {
      console.log(error)
    }
  }
  
  render(){
    return (
      <div className={styles.login}>
        <img className={styles.logo} src={require('../assets/img/login/logo.png')} />
        <CInput 
          img={require('../assets/img/login/phone.png')} 
          onChange={this.onChange}
          type="tel"
          placeholder="请输入手机号码"
          name="phone"
          value={this.state.phone}
        />
        <CInput 
          img={require('../assets/img/login/pwd.png')} 
          onChange={this.onChange}
          type="password"
          placeholder="请输入密码"
          name="password"
          value={this.state.password}
        />
        <div onClick={this.handleSubmit} className={styles.submit}>
          登  录
        </div>
        <div className={styles.bottom}>
          <span onClick={()=>{ this.props.history.push('/register')}} >快速注册</span> ｜ <span onClick={()=>{ this.props.history.push('/forgetPwd')}}>忘记密码</span>
        </div>
        <div className={styles.slogon}>
          数字三一  智造未来！
        </div>
      </div>
    )
  }
}

export default withRouter(Login)